var JsonAward = cc.Sprite.extend({
    _HP : 0,
    _OHP : 0,
    _gold : 0,
    _effect : null,
    _bloodB : null,
    _bloodBar : null,
    _life : true,
    _pointX : -1,
    _pointY : -1,
    ///////////////////////
    _chooserFlag : null,
    ///////////////////////////////////////////////////////////
    ctor : function(filename,hp,gold){
        this._super(filename);
        this._OHP = this._HP = hp;
        this._gold = gold;
        this.addBloodBar();
        this.addTouchEvent();
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._super();
    },
    ////////////////////////////////////////////////////////////////////////
    addBloodBar : function(){
        var filename = MainResource("blood0.png"),xOff=0.12,yOff=-8;
        this._bloodB = new cc.Sprite( filename  );
        this._bloodB.setPosition( cc.p( this.width*xOff,this.height + yOff ) );
        this._bloodB.setAnchorPoint( cc.p(0,0.5) );
        this._bloodB.setVisible(false);
        this.addChild( this._bloodB  );
        filename = MainResource("blood1.png");
        this._bloodBar = new cc.Sprite( filename );
        this._bloodBar.setPosition( cc.p( this.width*xOff,this.height + yOff ) );
        this._bloodBar.setAnchorPoint( cc.p(0,0.5) );
        this._bloodBar.setVisible(false);
        this.addChild( this._bloodBar  );
    },
    updateBloodBar : function(){
        if(!this._bloodBar.isVisible()){
            this.visibleBloodBar(true);
            this.schedule(this.visibleSchedule,3);
        }
        this._bloodBar.setScaleX( this._HP/this._OHP );
    },
    visibleSchedule:function(dt){
        this.visibleBloodBar(false);
    },
    visibleBloodBar:function(visible){
        if(visible){
            this._bloodBar.setVisible(true);
            this._bloodB.setVisible(true);
        }else{
            this._bloodBar.setVisible(false);
            this._bloodB.setVisible(false);
        }
    },
    ////////////////////////////////////////////////////////////////////////
    rect:function () {
        var tile = 70;
        return cc.rect(-tile * 0.5, -tile * 0.5, tile, tile);
    },
    collideRect:function (x, y) {
        var width = 70, height=70;
        return cc.rect(x-width*0.5, y-height*0.5, width, height);
    },
    containsTouchLocation:function (touch) {
        var getPoint = touch.getLocation();
        var myRect = this.rect();

        myRect.x += this.x;
        myRect.y += this.y;
        return cc.rectContainsPoint(myRect, getPoint);//this.convertTouchToNodeSpaceAR(touch));
    },
    addTouchEvent : function() {
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan
        }, this);
    },
    onTouchBegan : function (touch, event) {
        //return false;//////////////////////////////////////
        var target = event.getCurrentTarget();
        if (!target.containsTouchLocation(touch)) {
            return false;
        }else{
            {
                g_placeProxy.showPanel(false);
                g_updateProxy.showPanel(false);
            }
            cc.log("click at award!");
            ShowChooseFlag(true,target);
            return true;
        }
    },
    getPoint : function(){
        return cc.p(this._pointX,this._pointY);
    },
    setPoint : function(point){
        this._pointX=point.x,this._pointY=point.y;
    },
    hurt : function(hp){
        cc.log( "award hurt,hp: "+(this._HP-1) );
        this.hurtEffect();
        if( --this._HP<=0 ){
            // get it's gold to my money packet
            var iu = getUILayerInstance();
            iu.addMoney( this._gold );

            Unregister1(this.getPoint());
            this.dead();

            return true;
        }else{
            this.updateBloodBar();
        }
        return false;
    },
    dead : function(){
        this._life = false;
        this.removeFromParent(true);

        if( IsSameTarget(this) ){
            ShowChooseFlag(false);
        }
        /*var map = GetJsonMap();
        var awards = map.getAwardArray();
        awards.splice(this.getTag(), 1);// turn to checking in loop and remove //*/
    },
    hurtEffect : function(){
        cc.log("award effect");
    }
});


